package com.weetop.aklyscan.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.classic.adapter.BaseAdapterHelper;
import com.classic.adapter.CommonRecyclerAdapter;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.weetop.aklyscan.R;
import com.weetop.aklyscan.base.BaseActivity;
import com.weetop.aklyscan.base.mvp.BaseModel;
import com.weetop.aklyscan.bean.InventoryOrderBean;
import com.weetop.aklyscan.bean.SerailBean;
import com.weetop.aklyscan.presenter.InventoryPresent;
import com.weetop.aklyscan.view.InventoryView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class InventoryVerificationActivity extends BaseActivity<InventoryPresent> implements InventoryView {
    String type, code;
    int page;
    @BindView(R.id.recyclerView1)
    RecyclerView recyclerView1;
    @BindView(R.id.recyclerView2)
    RecyclerView recyclerView2;
    @BindView(R.id.num)
    TextView num;
    @BindView(R.id.code1)
    TextView code1;
    @BindView(R.id.code2)
    TextView code2;
    @BindView(R.id.ll1)
    LinearLayout ll1;
    @BindView(R.id.ll2)
    LinearLayout ll2;
    @BindView(R.id.recyclerView_ll1)
    LinearLayout recyclerView_ll1;
    @BindView(R.id.recyclerView_ll2)
    LinearLayout recyclerView_ll2;

    private CommonRecyclerAdapter<SerailBean.DataBean> orderAdapter1;
    private List<SerailBean.DataBean> orderList1 = new ArrayList<>();

    private CommonRecyclerAdapter<SerailBean.DataBean> orderAdapter2;
    private List<SerailBean.DataBean> orderList2 = new ArrayList<>();
    String[] strList;

    @Override
    protected InventoryPresent createPresenter() {
        return new InventoryPresent(this, this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inventory_verification);
        ButterKnife.bind(this);
        type = getIntent().getStringExtra("type");
        code = getIntent().getStringExtra("code");
        recyclerView1.setLayoutManager(new LinearLayoutManager(mActivity));
        recyclerView2.setLayoutManager(new LinearLayoutManager(mActivity));
        orderAdapter1 = new CommonRecyclerAdapter<SerailBean.DataBean>(mActivity, R.layout.item_inventory_verification, orderList1) {
            @Override
            public void onUpdate(BaseAdapterHelper helper, SerailBean.DataBean item, int position) {
                helper.setText(R.id.tv_xuhao, position + 1 + "");
                if (type.equals("0")) {
                    helper.setText(R.id.sn, item.getSerial_code());
                    helper.setText(R.id.model, item.getGoods_model());
                    helper.setText(R.id.name, item.getGoods_name());
                    helper.setText(R.id.zzx, item.getTransit_code());
                    helper.setText(R.id.hj_code, item.getWarehouse_coordinates());
                } else {
                    helper.setText(R.id.sn, item.getMatter_code());
                    helper.setText(R.id.model, item.getMatter_name());
                    helper.setText(R.id.name, item.getWp_num());
                    helper.setText(R.id.zzx, item.getTransit_code());
                    helper.setText(R.id.hj_code, item.getWarehouse_coordinates());
                }
            }
        };

        orderAdapter2 = new CommonRecyclerAdapter<SerailBean.DataBean>(mActivity, R.layout.item_inventory_verification1, orderList2) {
            @Override
            public void onUpdate(BaseAdapterHelper helper, SerailBean.DataBean item, int position) {
                helper.setText(R.id.tv_xuhao, position + 1 + "");
                if (type.equals("0")) {
                    helper.setText(R.id.sn, item.getSerial_code());
                    helper.setText(R.id.model, item.getGoods_model());
                    helper.setText(R.id.name, item.getGoods_name());
                    helper.setText(R.id.zzx, item.getTransit_code());
                    helper.setText(R.id.hj_code, item.getWarehouse_coordinates());
                } else {
                    helper.setText(R.id.sn, item.getMatter_code());
                    helper.setText(R.id.model, item.getWp_num());
                    helper.setText(R.id.name, item.getMatter_name());
                    helper.setText(R.id.zzx, item.getTransit_code());
                    helper.setText(R.id.hj_code, item.getWarehouse_coordinates());
                }
            }
        };
        recyclerView1.setAdapter(orderAdapter1);
        recyclerView2.setAdapter(orderAdapter2);

        if (type.equals("0")) {
            ll1.setVisibility(View.VISIBLE);
            ll2.setVisibility(View.GONE);
            recyclerView_ll1.setVisibility(View.VISIBLE);
            recyclerView_ll2.setVisibility(View.GONE);
            mPresenter.Into48(code);
        } else {
            ll1.setVisibility(View.GONE);
            ll2.setVisibility(View.VISIBLE);
            recyclerView_ll1.setVisibility(View.GONE);
            recyclerView_ll2.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public void getInto48Success(BaseModel<InventoryOrderBean> o) {
        InventoryOrderBean bean = o.getData();
        if (bean.getItems().size() > 0) {
            strList = new String[bean.getItems().size()];
            for (int i = 0; i < bean.getItems().size(); i++) {
                strList[i] = bean.getItems().get(i).getGoods_model();
            }
            code1.setText(bean.getItems().get(0).getGoods_model());
            page = 0;
            mPresenter.serail(bean.getItems().get(0).getGoods_model());
        }
    }

    @Override
    public void getserailSuccess(BaseModel<SerailBean> o) {
        List<SerailBean.DataBean> serailBeans = o.getData().getList();
        orderList1.clear();
        orderAdapter1.clear();
        orderList1.addAll(serailBeans);
        orderAdapter1.addAll(orderList1);
        num.setText(serailBeans.size() + "");
    }

    @Override
    public void getInto66Success(BaseModel<SerailBean> o) {
        List<SerailBean.DataBean> serailBeans = o.getData().getList_data();
        orderList2.clear();
        orderAdapter2.clear();
        orderList2.addAll(serailBeans);
        orderAdapter2.addAll(orderList2);
        num.setText(serailBeans.size() + "");
    }

    @OnClick({R.id.iv_back, R.id.code1, R.id.chaxun})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.code1:
                if (strList.length > 0) {
                    XPopup.get(InventoryVerificationActivity.this).asCenterList("请选择类型", strList,
                            new OnSelectListener() {
                                @Override
                                public void onSelect(int position, String text) {
                                    LogUtils.e(text);
                                    code1.setText(text);
                                    page = position;
                                    mPresenter.serail(text);
                                }
                            })
                            .show();
                }
                break;
            case R.id.chaxun:
//                finish();
                if (StringUtils.isEmpty(code2.getText().toString().trim())) {
                    ToastUtils.showShort("配件编号不能为空");
                    return;
                }
                mPresenter.Into66(code, code2.getText().toString().trim());
                break;
        }
    }
}
